package com.zj.thread.completeFuture;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class AllofTest {

    public static void main(String[] args) {
        futureTest();
    }

    public static  void futureTest() {
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future1 finished!"+getStringDate());
            return "future1 finished!";
        });
        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(7000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future2 finished!"+getStringDate());
            return "future2 finished!";
        });
        System.out.println("submit end"+getStringDate());
        CompletableFuture<Void> combindFuture = CompletableFuture.allOf(future1, future2);
        System.out.println("all end"+getStringDate());
        try {
            combindFuture.thenAcceptAsync((a)-> System.out.println("jjjj"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("future1: " + future1.isDone() + " future2: " + future2.isDone()+getStringDate());
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }
}
